<?php
require 'db_config.php';
require 'open_db.php';

include 'includes/header.php';

?>
<body>
<?php
if (isset($_POST['submit'])){
    $ing_1 = safe($_POST['ingredient_1']);
    if ($ing_1 == ""){
        echo "You must fill in the Ingredient name box.";
    } else {
        $exists = FALSE;
        $query = "SELECT ingredient_name FROM ingredient";
        $result = mysql_query($query);
        
        
        while($row= mysql_fetch_object($result)){
            if(strtolower($ing_1) == strtolower($row -> ingredient_name)){
            echo $ing_1 . " already exists.";
            $exists = TRUE;
            }
        }
        if(!$exists){
            $query = sprintf("INSERT INTO  `ingredient` (`ingredient_id` , `ingredient_name`) VALUES ( NULL ,  '%s' )", $ing_1);
            
            echo $query;
            mysql_query($query);

            echo "<p>Added " . $ing_1 . " to the ingredients list.</p>\n";

            echo "<p>Add another?</p>\n";
        }
    }
}
?>
<form action="ingredients.php" method="post">
<table>
<tr>
<td>
<p>Ingredient Name: <input name="ingredient_1" size="20" /></p>
</td>
</tr>
<tr>
<td>
<p><input type="submit" name="submit" value="submit" /></p>
</td>
</tr>
</table>
</form>
</body>
</html>
